<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Tags;
use Illuminate\Http\Request;

class TagController extends Controller {

    public function index(){
        $lists = Tags::tags();
        return $this->writeJson(0,$lists);
    }

    public function update(Request $request)
    {
        $name = (string)$request->get('name','');
        $tag = Tags::where('name',$name)->first();
        if($tag){
            return $this->writeJson(1,null,'标签名称已存在');
        }
        Tags::create(['name'=>$name]);
        return $this->writeJson(0,null,'新增标签信息成功');
    }

    public function destroy(Request $request){
        $id = (int)$request->get('id',0);
        if(empty($id)){
            return $this->writeJson(1,null,'参数错误');
        }
        Tags::destroy($id);
        return $this->writeJson(0);
    }

}
